/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.NewsDAOServices;
import model.entities.News;
import model.entities.NewsType;
import model.entities.Role;
import model.entities.User;


/**
 *
 * @author Hieu.Nguyen
 */
public class NewsDAO implements NewsDAOServices{
    private static NewsDAO newsDAO;
    Connection con;
    public static NewsDAO getInstance(){
        if(newsDAO==null){
            newsDAO= new NewsDAO();
        }
        return newsDAO;
    }

    @Override
    public List<News> getAllNews() {
        List<News> listNews = new ArrayList<>();
        try {
            con = ConnectionFactory.getConnection();
            String sql = "SELECT * FROM tblNews ";
            PreparedStatement pstmt = con.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                News nw = new News();
                nw.setIdNews(rs.getInt("idNews"));
                nw.setTitle(rs.getString("title"));
                nw.setSumary(rs.getString("sumary"));
                nw.setContents(rs.getString("contents"));
                nw.setImage(rs.getString("image"));
                
                NewsType nt = NewsTypeDAO.getInstance().getById(rs.getInt("idNewsType"));
                nw.setNewsType(nt);
                
                
                nw.setActived(rs.getBoolean("isActived"));
              
                User us = UserDAO.getInstance().getByUserId(rs.getInt("idUser"));              
                nw.setUser(us);
//                
                nw.setDateNews(rs.getDate("date"));
                listNews.add(nw); 
            }
            
        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listNews;
        
    }

    @Override
    public News getByNewsID(int idNews) {
        News newsss = null;
        try {
            String sql = "select * from tblNews where idNews = ?";
            newsss = new News();
            con = ConnectionFactory.getConnection();
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, idNews);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
            newsss.setIdNews(rs.getInt("idNews"));
            newsss.setActived(rs.getBoolean("isActived"));
            newsss.setTitle(rs.getString("title"));
            newsss.setSumary(rs.getString("sumary"));
            newsss.setContents(rs.getString("contents"));
            newsss.setImage(rs.getString("image"));
            NewsType nt = NewsTypeDAO.getInstance().getById(rs.getInt("idNewsType"));
            newsss.setNewsType(nt);
            User us = UserDAO.getInstance().getByUserId(rs.getInt("idUser"));
            newsss.setUser(us);
            newsss.setDateNews(rs.getDate("date"));
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return newsss;
    }


    @Override
    public User getByUserID(int idUser) {
        User us = new User();
        String sql  = "select * from tblUser where idUser= ?";
        try {
            con = ConnectionFactory.getConnection();
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, idUser);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                us.setIdUser(rs.getInt("idUser"));
                us.setUserName(rs.getString("userName"));
                us.setPassword(rs.getString("password"));
                us.setFullName(rs.getString("fullName"));
                us.setDateOfBirth(rs.getDate("dateOfBirth"));
                us.setGender(rs.getBoolean("gender"));
                us.setHomeTown(rs.getString("homeTown"));
                us.setSchool(rs.getString("school"));
                us.setPhoneNumber(rs.getString("phoneNumber"));
                us.setEmail(rs.getString("email"));
                us.setStrength(rs.getString("strength"));
                us.setLanguage(rs.getString("language"));
                us.setImage(rs.getString("image"));
                us.setAbout(rs.getString("about"));
                us.setFarvoriteQuotations(rs.getString("favoriteQuotations"));
                us.setFacebookLink(rs.getString("facebookLink"));
                us.setActiveCode(rs.getString("activeCode"));
                Role rl = RoleDAO.getInstance().getById(rs.getInt("idRole"));
                us.setRole(rl);
                us.setIsActived(rs.getBoolean("isActived"));
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return us;
    }



    @Override
    public NewsType getByNewsTypeID(int idNewsType) {
        NewsType nt = new NewsType();
        String sql  = "select * from tblNewsType where idNewType= ?";
        try {
            con = ConnectionFactory.getConnection();
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, idNewsType);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                nt.setIdNewType(rs.getInt("idNewType"));
                nt.setName(rs.getString("name"));
                nt.setDescriptions(rs.getString("descriptions"));
                nt.setIsActived(rs.getBoolean("isActived"));
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nt;
        
    }


    @Override
    public boolean createNews(News news) {
        boolean isCheck = false;
        try {
            con = ConnectionFactory.getConnection();
            String sql = "insert into tblNews values(?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, news.getTitle());
            pstmt.setString(2, news.getSumary());
            pstmt.setString(3, news.getContents());
            pstmt.setString(4, news.getImage());
            pstmt.setInt(5, news.getNewsType().getIdNewType());
            pstmt.setBoolean(6, news.isActived());
            pstmt.setInt(7, news.getUser().getIdUser());
            pstmt.setDate(8, (Date) news.getDateNews());
            pstmt.executeUpdate();
            isCheck = true;
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;

    }





    @Override
    public boolean enableNews(int idNews, boolean isActived) {
        boolean isCheck = false;
        try {
            con = ConnectionFactory.getConnection();
            String sql = "update tblNews set isActived = ? where  idNews = ? ";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setBoolean(1, isActived);
            pstmt.setInt(2, idNews);
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }

    @Override
    public boolean deleteNews(int idNews) {
        boolean isCheck = false;
        try {
            con = ConnectionFactory.getConnection();
            String sql = "delete tblNews  where idNews = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, idNews);
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }

    @Override
    public boolean updateNews(News news) {
        boolean isCheck = false;
        try {
            String sql = "update tblNews set title = ?, sumary = ?, contents = ?,image = ?, idNewsType = ?, isActived = ?, idUser = ?, date = ? where idNews = ?";
            con = ConnectionFactory.getConnection();
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, news.getTitle());
            pstmt.setString(2, news.getSumary());
            pstmt.setString(3, news.getContents());
            pstmt.setString(4, news.getImage());
            pstmt.setInt(5, news.getNewsType().getIdNewType());
            pstmt.setBoolean(6, news.isActived());
            pstmt.setInt(8, news.getUser().getIdUser());
            pstmt.setDate(7, (Date) news.getDateNews());
            
            pstmt.executeUpdate();
            
            isCheck = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }

    
}
